//package com.zyc.demo.rabbitmq.config;//package com.zyc.demo.rabbitmq.config;
//
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.amqp.core.*;
//import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
//import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
//import org.springframework.amqp.rabbit.connection.ConnectionFactory;
//import org.springframework.amqp.rabbit.connection.CorrelationData;
//import org.springframework.amqp.rabbit.core.RabbitTemplate;
//import org.springframework.amqp.rabbit.listener.RabbitListenerContainerFactory;
//import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
//import org.springframework.amqp.support.converter.MessageConversionException;
//import org.springframework.amqp.support.converter.MessageConverter;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.core.env.Environment;
//
//import java.io.ByteArrayInputStream;
//import java.io.IOException;
//import java.io.ObjectInputStream;
//import java.util.Map;
//
///**
// * 参考链接：https://blog.csdn.net/u013871100/article/details/82982235
// */
//@Configuration
//public class RabbitmqConfig {
//
//    private static final Logger log = LoggerFactory.getLogger(RabbitmqConfig.class);
//    @Autowired
//    private Environment env;
//    @Autowired
//    private CachingConnectionFactory connectionFactory;
//    @Autowired
//    private SimpleRabbitListenerContainerFactoryConfigurer factoryConfigurer;
//
//    /**
//     * 单一消费者
//     *
//     * @return
//     */
//    @Bean(name = "singleListenerContainer")
//    public SimpleRabbitListenerContainerFactory listenerContainer() {
//        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
//        factory.setConnectionFactory(connectionFactory);
//        factory.setMessageConverter(new Jackson2JsonMessageConverter());
//        factory.setConcurrentConsumers(1);
//        factory.setMaxConcurrentConsumers(1);
//        factory.setPrefetchCount(1);
//        factory.setTxSize(1);
//        factory.setAcknowledgeMode(AcknowledgeMode.AUTO);
//        return factory;
//    }
//
//    /**
//     * 多个消费者
//     *
//     * @return
//     */
//    @Bean(name = "multiListenerContainer")
//    public SimpleRabbitListenerContainerFactory multiListenerContainer() {
//        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
//        factoryConfigurer.configure(factory, connectionFactory);
//        factory.setMessageConverter(new Jackson2JsonMessageConverter());
//        factory.setAcknowledgeMode(AcknowledgeMode.NONE);
//        factory.setConcurrentConsumers(env.getProperty("spring.rabbitmq.listener.concurrency", int.class));
//        factory.setMaxConcurrentConsumers(env.getProperty("spring.rabbitmq.listener.max-concurrency", int.class));
//        factory.setPrefetchCount(env.getProperty("spring.rabbitmq.listener.prefetch", int.class));
//        return factory;
//    }
//
//    @Bean
//    public RabbitTemplate rabbitTemplate() {
//        connectionFactory.setPublisherConfirms(true);
//        connectionFactory.setPublisherReturns(true);
//        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
//        rabbitTemplate.setMandatory(true);
////        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
////            @Override
////            public void confirm(CorrelationData correlationData, boolean ack, String cause) {
////                log.info("消息发送成功:correlationData({}),ack({}),cause({})",correlationData,ack,cause);
////            }
////        });
//        rabbitTemplate.setConfirmCallback(
//                (CorrelationData correlationData, boolean ack, String cause)
//                        ->
//                        log.info("消息发送成功:correlationData({}),ack({}),cause({})",correlationData,ack,cause));
////        rabbitTemplate.setReturnCallback(new RabbitTemplate.ReturnCallback() {
////            @Override
////            public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
////                log.info("消息丢失:exchange({}),route({}),replyCode({}),replyText({}),message:{}",exchange,routingKey,replyCode,replyText,message);
////            }
////        });
//        rabbitTemplate.setReturnCallback(
//                (Message message, int replyCode, String replyText, String exchange, String routingKey)
//                        ->
//                        log.info("消息丢失:exchange({}),route({}),replyCode({}),replyText({}),message:{}",
//                                exchange,routingKey,replyCode,replyText,message));
//        return rabbitTemplate;
//    }
//
//
//
//    // 用户操作日志消息模型
//    @Bean
//    public Queue logUserQueue(){
//        String queueName = env.getProperty("log.user.queue.name");
//        return new Queue(queueName,true,false,false,null);
//    }
//
//    @Bean
//    public DirectExchange logUserExchange(){
//        String exchangeName = env.getProperty("log.user.exchange.name");
//        return new DirectExchange(exchangeName,true,true,null);
//    }
//
//    @Bean
//    public Binding logUserBinding(){
//        String routingKey = env.getProperty("log.user.routing.key.name");
//        return BindingBuilder.bind(logUserExchange()).to(logUserExchange()).with(routingKey);
//    }
//
//
//    @Bean
//    public RabbitListenerContainerFactory<?> rabbitListenerContainerFactory(ConnectionFactory connectionFactory){
//        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
//        factory.setConnectionFactory(connectionFactory);
//        factory.setMessageConverter(new MessageConverter() {
//            @Override
//            public Message toMessage(Object object, MessageProperties messageProperties) throws MessageConversionException {
//                return null;
//            }
//
//            @Override
//            public Object fromMessage(Message message) throws MessageConversionException {
//                ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(message.getBody());
//                ObjectInputStream ois = null;
//                try {
//                    ois = new ObjectInputStream(byteArrayInputStream);
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//                Object o = null;
//                try {
//                    o = ois.readObject();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                } catch (ClassNotFoundException e) {
//                    e.printStackTrace();
//                }
//                return o;
//            }
//        });
//        return factory;
//    }
//
//
//}
